package com.yssoft.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * 
    * @ClassName: ExcelUtil
    * @Description: 动态数据导出，支持列动态
    * @author ljy
    * @date 2018年6月25日
    *
 */
public class ExcelUtil {
	/**
	 * 
	    * @Title: baseExportExcel
	    * @Description: 基本格式的数据导出
	    * @param @param data  导出数据源
	    * @param @param excelHeader 表格头信息数组
	    * @return void    返回类型
	    * @throws
	 */
	public static Workbook baseExportExcel(  List<Map<String, Object>> dataList,String[] headers,String sheetName) {
			// 声明一个工作薄
			XSSFWorkbook  workbook = new XSSFWorkbook();
			// 生成一个表格
			XSSFSheet sheet = workbook.createSheet(sheetName);
			// 设置表格默认列宽度为15个字节
			sheet.setDefaultColumnWidth(40);
			// 生成表格中非标题栏的样式
			XSSFCellStyle style = workbook.createCellStyle();
			// 设置这些样式
			style.setFillForegroundColor(HSSFColor.WHITE.index);//背景色
			style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			style.setBorderBottom(BorderStyle.THIN);
			style.setBorderLeft(BorderStyle.THIN);
			style.setBorderRight(BorderStyle.THIN);
			style.setBorderTop(BorderStyle.THIN);
			style.setAlignment(HorizontalAlignment.CENTER);
			// 生成表格中非标题栏的字体
			XSSFFont font = workbook.createFont();
			font.setColor(HSSFColor.BLACK.index);
			font.setFontHeightInPoints((short) 12);
			font.setBold(true);
			// 把字体应用到当前的样式
			style.setFont(font);
			// 设置表格标题栏的样式
			XSSFCellStyle titleStyle = workbook.createCellStyle();
			titleStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index);
			titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			titleStyle.setBorderBottom(BorderStyle.THIN);
			titleStyle.setBorderLeft(BorderStyle.THIN);
			titleStyle.setBorderRight(BorderStyle.THIN);
			titleStyle.setBorderTop(BorderStyle.THIN);
			titleStyle.setAlignment(HorizontalAlignment.CENTER);
			titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
			// 设置标题栏字体
			XSSFFont titleFont = workbook.createFont();
			titleFont.setColor(HSSFColor.WHITE.index);
			titleFont.setFontHeightInPoints((short) 12);
			titleFont.setBold(true);
			// 把字体应用到当前的样式
			titleStyle.setFont(titleFont);
			
			// 产生表格标题行
			XSSFRow row = sheet.createRow(0);
			for (short i = 0; i < headers.length; i++) {
				XSSFCell cell = row.createCell(i);
				cell.setCellStyle(titleStyle);
				XSSFRichTextString text = new XSSFRichTextString(headers[i]);
				cell.setCellValue(text);
			}
			
	        // 遍历集合数据，产生数据行
			if(dataList!=null && dataList.size()>0) {
				Iterator<Map<String, Object>> it = dataList.iterator();
				int index = 0;
				while (it.hasNext()) {
					index++;
					row = sheet.createRow(index);
					Map<String, Object> data = it.next();
					int i = 0;
					/*excelMap.put("code", code);
					excelMap.put("url", url);*/
					for(String key : data.keySet()){
						if("code".equals(key)) {
							XSSFCell cell = row.createCell(0);
							cell.setCellStyle(style);
							XSSFRichTextString text = new XSSFRichTextString(data.get(key)+"");
							cell.setCellValue(text);
						}
						if("url".equals(key)) {
							XSSFCell cell = row.createCell(1);
							cell.setCellStyle(style);
							XSSFRichTextString text = new XSSFRichTextString(data.get(key)+"");
							cell.setCellValue(text);
						}
						/*XSSFCell cell = row.createCell(i);
						cell.setCellStyle(style);
						XSSFRichTextString text = new XSSFRichTextString(data.get(key)+"");
						cell.setCellValue(text);
						i++;*/
					}
				}
			}
			return workbook;
			 
	}
	
}
